package org.studiox.graph.persistence.mybatis.tk.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.studiox.graph.persistence.mybatis.tk.dao.GraphEdgeDao;
import org.studiox.graph.persistence.mybatis.tk.dao.GraphVertexDao;
import org.studiox.graph.persistence.mybatis.tk.domain.GraphEdgeDO;
import org.studiox.graph.persistence.mybatis.tk.domain.GraphVertexDO;

import java.util.Collections;
import java.util.List;

@Service
public class GraphDbService {

  private static final Logger LOGGER = LoggerFactory.getLogger(GraphDbService.class);

  @Autowired private GraphVertexDao graphVertexDao;

  @Autowired private GraphEdgeDao graphEdgeDao;

  public Integer getGraphVertexSize() {
    try {
      return graphVertexDao.getGraphVertexSize();
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
    return null;
  }

  public List<Long> getGraphVertexIds() {
    try {
      return graphVertexDao.getGraphVertexIds();
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
    return Collections.emptyList();
  }

  public List<String> getGraphVertexUniqs() {
    try {
      return graphVertexDao.getGraphVertexUniqs();
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
    return Collections.emptyList();
  }

  public Long getGraphVertexIdByUniq(String uniq) {
    try {
      return graphVertexDao.getGraphVertexIdByUniq(uniq);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
    return null;
  }

  public Long getJanusVertexIdByUniq(String uniq) {
    try {
      return graphVertexDao.getJanusVertexIdByUniq(uniq);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
    return null;
  }

  public GraphVertexDO getGraphVertexDoById(Long dbId) {
    try {
      return graphVertexDao.selectByPrimaryKey(dbId);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
    return null;
  }

  public GraphVertexDO getGraphVertexDoByUniq(String uniq) {
    try {
      return graphVertexDao.getGraphVertexDoByUniq(uniq);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
    return null;
  }

  public List<GraphVertexDO> getGraphVertexDosByUniqLike(String likeUniqStr) {
    try {
      return graphVertexDao.getGraphVertexDosByUniqLike(likeUniqStr);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
    return Collections.emptyList();
  }

  public void insertGraphVertex(GraphVertexDO graphVertexDO) {
    try {
      graphVertexDao.insertSelective(graphVertexDO);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateGraphVertex(GraphVertexDO graphVertexDO) {
    try {
      graphVertexDao.updateByPrimaryKeySelective(graphVertexDO);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateJanusVertexId(Long id, Long janusVertexId) {
    try {
      graphVertexDao.updateJanusVertexId(id, janusVertexId);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateNeo4jVertexId(Long id, Long neo4jVertexId) {
    try {
      graphVertexDao.updateNeo4jVertexId(id, neo4jVertexId);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateNebulaVertexId(Long id, Long nebulaVertexId) {
    try {
      graphVertexDao.updateNebulaVertexId(id, nebulaVertexId);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void deleteGraphVertexByDbId(Long dbId) {
    try {
      graphVertexDao.deleteGraphVertexByDbId(dbId);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public Integer getGraphEdgeSize() {
    try {
      return graphEdgeDao.getGraphEdgeSize();
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
    return null;
  }

  public List<Long> getGraphEdgeIds() {
    try {
      return graphEdgeDao.getGraphEdgeIds();
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
    return Collections.emptyList();
  }

  public List<Long> getGraphUniqueMinEdgeIds() {
    try {
      return graphEdgeDao.getGraphUniqueMinEdgeIds();
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
    return Collections.emptyList();
  }

  public List<Long> getGraphUniqueMaxEdgeIds() {
    try {
      return graphEdgeDao.getGraphUniqueMaxEdgeIds();
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
    return Collections.emptyList();
  }

  public Long getGraphEdgeContentSize(String sourceVertexUniq, String targetVertexUniq) {
    try {
      return graphEdgeDao.getGraphEdgeContentSize(sourceVertexUniq, targetVertexUniq);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
    return null;
  }

  public List<String> getGraphEdgeContents(String sourceVertexUniq, String targetVertexUniq) {
    try {
      return graphEdgeDao.getGraphEdgeContents(sourceVertexUniq, targetVertexUniq);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
    return Collections.emptyList();
  }

  public List<GraphEdgeDO> getGraphEdgeDOs(GraphEdgeDO graphEdgeDO) {
    try {
      return graphEdgeDao.getGraphEdgeDOs(graphEdgeDO);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
    return Collections.emptyList();
  }

  public GraphEdgeDO getOneGraphEdgeDoBySourceTargetUniq(
      String sourceVertexUniq, String targetVertexUniq) {
    try {
      return graphEdgeDao.getOneGraphEdgeDoBySourceTargetUniq(sourceVertexUniq, targetVertexUniq);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
    return null;
  }

  public Long getGraphEdgeId(String content, String sourceVertexUniq, String targetVertexUniq) {
    try {
      return graphEdgeDao.getGraphEdgeId(content, sourceVertexUniq, targetVertexUniq);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
    return null;
  }

  public GraphEdgeDO getGraphEdgeDoById(Long dbId) {
    try {
      return graphEdgeDao.selectByPrimaryKey(dbId);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
    return null;
  }

  public void insertGraphEdge(GraphEdgeDO graphEdgeDO) {
    try {
      graphEdgeDao.insertSelective(graphEdgeDO);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateGraphEdge(GraphEdgeDO graphEdgeDO) {
    try {
      graphEdgeDao.updateByPrimaryKeySelective(graphEdgeDO);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateGraphEdgeContentMd5(Long dbId) {
    try {
      graphEdgeDao.updateGraphEdgeContentMd5(dbId);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateAllGraphEdgeContentMd5() {
    try {
      graphEdgeDao.updateAllGraphEdgeContentMd5();
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateJanusEdgeId(Long id, String janusEdgeId) {
    try {
      graphEdgeDao.updateJanusEdgeId(id, janusEdgeId);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateJanusEdgeIdAndSourceTargetVertexId(
      Long id, String janusEdgeId, Long sourceJanusVertexId, Long targetJanusVertexId) {
    try {
      graphEdgeDao.updateJanusEdgeIdAndSourceTargetVertexId(
          id, janusEdgeId, sourceJanusVertexId, targetJanusVertexId);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateNeo4jEdgeId(Long id, Long neo4jEdgeId) {
    try {
      graphEdgeDao.updateNeo4jEdgeId(id, neo4jEdgeId);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateNeo4jEdgeIdAndSourceTargetVertexId(
      Long id, Long neo4jEdgeId, Long sourceNeo4jVertexId, Long targetNeo4jVertexId) {
    try {
      graphEdgeDao.updateNeo4jEdgeIdAndSourceTargetVertexId(
          id, neo4jEdgeId, sourceNeo4jVertexId, targetNeo4jVertexId);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateNebulaEdgeId(Long id, String nebulaEdgeId) {
    try {
      graphEdgeDao.updateNebulaEdgeId(id, nebulaEdgeId);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateNebulaEdgeIdAndSourceTargetVertexId(
      Long id, String nebulaEdgeId, Long sourceNebulaVertexId, Long targetNebulaVertexId) {
    try {
      graphEdgeDao.updateNebulaEdgeIdAndSourceTargetVertexId(
          id, nebulaEdgeId, sourceNebulaVertexId, targetNebulaVertexId);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateSourceJanusVertexId(String sourceVertexUniq, Long sourceJanusVertexId) {
    try {
      graphEdgeDao.updateSourceJanusVertexId(sourceVertexUniq, sourceJanusVertexId);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateTargetJanusVertexId(String sourceVertexUniq, Long targetJanusVertexId) {
    try {
      graphEdgeDao.updateTargetJanusVertexId(sourceVertexUniq, targetJanusVertexId);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateAllSourceJanusVertexId() {
    try {
      graphEdgeDao.updateAllSourceJanusVertexId();
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateAllTargetJanusVertexId() {
    try {
      graphEdgeDao.updateAllTargetJanusVertexId();
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateAllSourceTargetJanusVertexId() {
    try {
      graphEdgeDao.updateAllSourceTargetJanusVertexId();
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateSourceNeo4jVertexId(String sourceVertexUniq, Long sourceNeo4jVertexId) {
    try {
      graphEdgeDao.updateSourceNeo4jVertexId(sourceVertexUniq, sourceNeo4jVertexId);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateTargetNeo4jVertexId(String sourceVertexUniq, Long targetNeo4jVertexId) {
    try {
      graphEdgeDao.updateTargetNeo4jVertexId(sourceVertexUniq, targetNeo4jVertexId);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateAllSourceNeo4jVertexId() {
    try {
      graphEdgeDao.updateAllSourceNeo4jVertexId();
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateAllTargetNeo4jVertexId() {
    try {
      graphEdgeDao.updateAllTargetNeo4jVertexId();
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateAllSourceTargetNeo4jVertexId() {
    try {
      graphEdgeDao.updateAllSourceTargetNeo4jVertexId();
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateSourceNebulaVertexId(String sourceVertexUniq, Long sourceNebulaVertexId) {
    try {
      graphEdgeDao.updateSourceNebulaVertexId(sourceVertexUniq, sourceNebulaVertexId);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateTargetNebulaVertexId(String sourceVertexUniq, Long targetNebulaVertexId) {
    try {
      graphEdgeDao.updateTargetNebulaVertexId(sourceVertexUniq, targetNebulaVertexId);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateAllSourceNebulaVertexId() {
    try {
      graphEdgeDao.updateAllSourceNebulaVertexId();
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateAllTargetNebulaVertexId() {
    try {
      graphEdgeDao.updateAllTargetNebulaVertexId();
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateAllSourceTargetNebulaVertexId() {
    try {
      graphEdgeDao.updateAllSourceTargetNebulaVertexId();
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateGraphVertexDbUniq(String oldUniq, String newUniq) {
    try {
      GraphVertexDO graphVertexDO = graphVertexDao.getGraphVertexDoByUniq(oldUniq);
      if (null != graphVertexDO) {
        graphVertexDO.setUniq(newUniq);
        graphVertexDao.updateByPrimaryKeySelective(graphVertexDO);
      }
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void updateGraphEdgeDbSourceTargetUniq(String oldUniq, String newUniq) {
    GraphEdgeDO sourceGraphEdgeDO = new GraphEdgeDO();
    sourceGraphEdgeDO.setSourceVertexUniq(oldUniq);
    try {
      List<GraphEdgeDO> graphEdgeDos = graphEdgeDao.select(sourceGraphEdgeDO);
      if (null != graphEdgeDos) {
        for (GraphEdgeDO obj : graphEdgeDos) {
          obj.setSourceVertexUniq(newUniq);
          graphEdgeDao.updateByPrimaryKeySelective(obj);
        }
      }
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
    GraphEdgeDO targetGraphEdgeDO = new GraphEdgeDO();
    targetGraphEdgeDO.setTargetVertexUniq(oldUniq);
    try {
      List<GraphEdgeDO> graphEdgeDos = graphEdgeDao.select(targetGraphEdgeDO);
      if (null != graphEdgeDos) {
        for (GraphEdgeDO obj : graphEdgeDos) {
          obj.setTargetVertexUniq(newUniq);
          graphEdgeDao.updateByPrimaryKeySelective(obj);
        }
      }
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }

  public void deleteGraphEdgeByDbId(Long dbId) {
    try {
      graphEdgeDao.deleteGraphEdgeByDbId(dbId);
    } catch (Throwable t) {
      LOGGER.error(t.getMessage(), t);
    }
  }
}
